//
//  ViewController.m
//  saveDataDemos
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *stuIdtextField;
@property (weak, nonatomic) IBOutlet UITextField *nametextField;
@property (weak, nonatomic) IBOutlet UITextField *AgetextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#define kStuId @"StuId"
#define kStuName @"StuName"
#define kStuAge @"StuAge"
- (IBAction)save1:(id)sender
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
   
    [d setObject:self.stuIdtextField.text forKey:kStuId];
    [d setObject:self.nametextField.text forKey:kStuName];
    [d setObject:self.AgetextField.text forKey:kStuAge];
    
    
    
    [d synchronize];
}
- (IBAction)read1:(id)sender
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    
    self.stuIdtextField.text = [d objectForKey:kStuId];
    self.nametextField.text = [d objectForKey:kStuName];
    self.AgetextField.text = [d objectForKey:kStuAge];

}
- (IBAction)save2:(id)sender
{
    NSString *docpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docpath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dict =@{@"stuId":self.stuIdtextField.text,@"stuName":self.nametextField.text,@"stuAge":self.AgetextField.text};
    
    [dict writeToFile:filePath atomically:YES];
    
    
}
- (IBAction)read2:(id)sender
{
   
    NSString *docpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docpath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    self.stuIdtextField.text = dic[@"stuId"];
    self.nametextField.text = dic[@"stuName"];
    self.AgetextField.text = dic[@"stuAge"];
    
}
- (IBAction)save3:(id)sender
{
    NSString *docpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docpath stringByAppendingPathComponent:@"student.arch"];
    StudentModel *stu = [[StudentModel alloc]initWithId:self.stuIdtextField.text andName:self.nametextField.text andAge:self.AgetextField.text];
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:stu forKey:@"Student"];
    [arch finishEncoding];
    [mData writeToFile:filePath atomically:YES];
}
- (IBAction)read3:(id)sender
{
    NSString *docpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docpath stringByAppendingPathComponent:@"student.arch"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSKeyedUnarchiver *unarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
    StudentModel *stu = [unarch decodeObjectForKey:@"Student"];
    
    [unarch finishDecoding];
    self.stuIdtextField.text = stu.stuId;
    self.nametextField.text = stu.stuName;
    self.AgetextField.text = stu.stuAge;

}

@end
